package cn.lbd.datastructure;

import java.util.Scanner;

class Test {
    public static void main(String[] args) {
        //创建哈希表
        HashTab hashTab = new HashTab(7);

        //写一个简单的菜单
        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("add:  添加雇员");
            System.out.println("list: 显示雇员");
            System.out.println("find: 查找雇员");
            System.out.println("exit: 退出系统");

            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name = scanner.next();
                    //创建 雇员
                    Emp emp = new Emp(id, name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入要查找的id");
                    id = scanner.nextInt();
                    hashTab.findById(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }

    }
}

/**
 * 哈希表，管理链表数组
 */
public class HashTab {
    private EmpLinkedList[] empLinkedListArray;
    private int size; //多少条链表

    /**
     * 易错点，不仅要分配链表数组的地址1，也要为每个链表去分配内存
     *
     * @param size 数组容量
     */
    public HashTab(int size) {
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }

    /**
     * @param id
     * @return 链表数组索引
     */
    public int hash(int id) {
        return id % size;
    }

    /**
     * 哈希表经过hash求出数组索引并插入元素
     *
     * @param emp
     */
    public void add(Emp emp) {
        int empLinkedListNO = hash(emp.id);
        EmpLinkedList empLinkedList = empLinkedListArray[empLinkedListNO];
        empLinkedList.add(emp);
    }

    /**
     * 遍历哈希表
     */
    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list(i);
        }
    }

    /**
     * 查找哈希表的元素
     *
     * @return
     */
    public void findById(int no) {
        int empLinkedListNO = hash(no);
        Emp emp = empLinkedListArray[empLinkedListNO].findById(no);
        if (emp == null) {
            System.out.println("未查找到元素");
        } else {
            System.out.printf("=> id = %d，name = %s\t", emp.id, emp.name);
        }
        System.out.println();
    }


}

/**
 * 每条链表
 */
class EmpLinkedList {
    private Emp head;

    //默认插入在链表最尾部
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = emp;
    }

    public void list(int no) {
        if (head == null) {
            System.out.println("链表" + (no + 1) + "找不到，请插入数据");
            return;
        }
        Emp temp = head;
        while (true) {
            System.out.printf("=> id = %d，name = %s\t", temp.id, temp.name);
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        System.out.println();
    }

    /**
     * 如果找到了返回当前Emp,没找到就返回Null
     *
     * @param id 要查找的元素的Id
     * @return
     */
    public Emp findById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Emp temp = head;
        while (true) {
            if (temp.id == id) {
                break;
            }
            temp = temp.next;
            if (temp.next == null) {
                temp = null;
                break;
            }
        }
        return temp;
    }
}

class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }
}